
package br.com.duduso.conferencia_claro_desktop.components;

/**
 *
 * @author duduso
 */
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;


public class TabButton extends JPanel {

    private final JTabbedPane pane;

    public TabButton(final JTabbedPane pane, Font font) {

            super(new FlowLayout(FlowLayout.LEFT, 0, 0));

            if (pane == null) {
                    throw new NullPointerException("TabPanel Está NULO!");
            }

            this.pane = pane;

            setOpaque(false);

            JLabel label = new JLabel() {
                    @Override
                    public String getText() {
                            int i = pane.indexOfTabComponent(TabButton.this);
                            if (i != -1) {
                                    return pane.getTitleAt(i);
                            }
                            return null;
                    }
            };
            label.setFont(font);

            add(label);
            label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

            JButton button = new ButtonTab();
            add(button);

            setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));

    }

    private class ButtonTab extends JButton implements ActionListener {

        public ButtonTab() {
                setPreferredSize(new Dimension(17, 17));
                setToolTipText("fechar");
                setUI(new BasicButtonUI());
                setContentAreaFilled(false);
                setFocusable(false);
                setBorder(BorderFactory.createEtchedBorder());
                setBorderPainted(false);
                addMouseListener(buttonMouseListener);
                setRolloverEnabled(true);
                addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
                pane.remove(pane.getSelectedIndex());
        }

        @Override
        public void updateUI() {
        }

        @Override
        protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g.create();
                if (getModel().isPressed()) {
                        g2.translate(1, 1);
                }
                g2.setStroke(new BasicStroke(2));
                g2.setColor(Color.BLACK);
                if (getModel().isRollover()) {
                        g2.setColor(Color.RED);
                }
                int delta = 6;
                g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
                g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
                g2.dispose();
        }
    }

    private final static MouseListener buttonMouseListener = new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                    Component component = e.getComponent();
                    if (component instanceof AbstractButton) {
                            AbstractButton button = (AbstractButton) component;
                            button.setBorderPainted(true);
                    }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                    Component component = e.getComponent();
                    if (component instanceof AbstractButton) {
                            AbstractButton button = (AbstractButton) component;
                            button.setBorderPainted(false);
                    }
            }
    };
}


